package com.dev.admin.web.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import com.dev.admin.entity.Project;
import com.dev.admin.service.ProjectService;
import com.dev.admin.vo.ProjectVo;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.sound.midi.Soundbank;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/project")
public class ProjectController {
    @Reference
    private ProjectService projectService;

    @RequestMapping("/addOrUpdateProject")
    public ResponseEntity addOrUpdateProject(ProjectVo project){
        if(project.getId()==0){
            projectService.addProject(project);
        }else{
            projectService.updateProject(project);
        }
        return ResponseEntity.ok(0);
    }

    @RequestMapping("/deleteProject")
    public ResponseEntity deleteProject(int id){
        int i = projectService.deleteProject(id);
        return ResponseEntity.ok(i);
    }

    @RequestMapping("/getProjectList")
    public ResponseEntity getProjectList(){
        List<Project> list = projectService.getProjectList();

        HashMap<String, Object> data = CollectionUtil.newHashMap();
        data.put("code",0);
        data.put("list",list);
        data.put("totol",list.size());
        return ResponseEntity.ok(data);
    }

    @PostMapping("/addProjectUser")
    public ResponseEntity addProjectUser(String userIds,
                                         @RequestParam(value = "projectId") int projectId){

        String users = userIds.substring(1,userIds.length() - 1);
        String[] split = users.split(",");
        for (int i = 0; i < split.length; i++) {
            projectService.addProjectUser(Integer.parseInt(split[i]),projectId);
        }
        return ResponseEntity.ok(0);
    }

    @RequestMapping("/getMyJoin")
    public ResponseEntity getMyJoin(int userId){
        System.out.println(userId+"=================");
        List<Integer> projects = projectService.getProjectIdByUserID((userId));
        List<Project> list = new ArrayList<>();
        for (int i = 0; i < projects.size(); i++) {
           list.add(projectService.getMyJoin(projects.get(i)));
        }
        HashMap<String, Object> data = CollectionUtil.newHashMap();
        data.put("code",0);
        data.put("list",list);
        data.put("totol",list.size());
        return ResponseEntity.ok(data);
    }
}


